summaryrefslogtreecommitdiff
path: root/ios/shared/ios_sharedlo/objective_c/view_controllers/toolbar/MLOToolbarButton.m
blob: 5576ec9a19f9c2325246a3c7f2353e5bb05baf7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#import "MLOToolbarButton.h"
#import "MLOToolbarViewController.h"
#import "MLOToolbarViewController_Impl.h"
#import "MLOMainViewController.h"
#import "MLOResourceImage.h"

static const CGFloat
    TAPPED_ALPHA=1.0f,
    UNTAPPED_ALPHA=0.3f,
    BUTTON_IMAGE_X_EDGE_OF_CANVAS_PADDING = 40.0f,
    BUTTON_IMAGE_Y_EDGE_OF_CANVAS_PADDING = 30.0f,
    BUTTON_IMAGE_Y_SPACING = 40.0f,
    BUTTON_THICKENING = 10.f,
    FADE_TO_UNTAPPED_TIME=0.5f;

static NSInteger toolbarButtonsTotalHeight=BUTTON_IMAGE_Y_EDGE_OF_CANVAS_PADDING;

static const BOOL IS_FLASH_ON_TAP = NO;

@interface MLOToolbarButton ()
@property MLOToolbarViewController * toolbarController;
@property (nonatomic,strong) MLOToolbarButtonCallback onTap, onTapRelease;
@property BOOL isTapped,currentImageIsMain;
@property MLOToolbarButtonTapReleaseType tapReleaseType;
@property MLOResourceImage * mainImage;
@end


@implementation MLOToolbarButton

-(void)addToToolbarControler:(MLOToolbarViewController *) toolbar{
    self.toolbarController = toolbar;
    
}
+(MLOToolbarButton *)buttonWithImage:(MLOResourceImage *) image onTap:(MLOToolbarButtonCallback) onTap tapRelease:(MLOToolbarButtonTapReleaseType)type{
    return [MLOToolbarButton buttonWithImage:image onTap:onTap tapRelease:type onTapRelease:MLO_TOOLBAR_BUTTON_STUB_CALLBACK];
}
+(MLOToolbarButton *)buttonWithImage:(MLOResourceImage *) image onTap:(MLOToolbarButtonCallback) onTap tapRelease:(MLOToolbarButtonTapReleaseType)type onTapRelease:(MLOToolbarButtonCallback)onTapRelease
{
    
    MLOToolbarButton * button = [MLOToolbarButton buttonWithType:UIButtonTypeCustom];
  
    if(button){

        button.mainImage = image;
        button.onTap = onTap;
        button.tapReleaseType = type;
        button.onTapRelease = onTapRelease;
        button.alternateImage=nil;
        
        static const CGFloat BUTTON_X=BUTTON_IMAGE_X_EDGE_OF_CANVAS_PADDING -BUTTON_THICKENING,
                BUTTON_TWICE_THICKENING=2.0f*BUTTON_THICKENING;
        
        button.frame = CGRectMake(BUTTON_X,
                                  toolbarButtonsTotalHeight-BUTTON_THICKENING,
                                  image.image.size.width + BUTTON_TWICE_THICKENING,
                                  image.image.size.height + BUTTON_TWICE_THICKENING);
        
        toolbarButtonsTotalHeight += image.image.size.height + BUTTON_IMAGE_Y_SPACING;

        [button reset:0];
        [button addAction:@selector(onTapAction)];
        
    }
    return button;
}

-(BOOL)isHold{
    return _tapReleaseType != AUTOMATIC;
}

-(void)releaseIfNeeded{
    if(_isTapped && [self isHold]){
        [self invokeOnTapRelease];
    }
    _isTapped = NO;
}

-(void)reset:(CGFloat) alpha{
    [self releaseIfNeeded];
    self.alpha = alpha;
}

-(void)onOtherButtonTapped{
    if(_tapReleaseType == RETAP_OR_OTHER_TAPPED){
        [self releaseIfNeeded];
        [self fadeToUntapped];
    }
}

-(void)invokeOnTapRelease{
    [self invoke:_onTapRelease named:@"onTapRelease"];
}

-(void)switchImage{
    if(_alternateImage!=nil){
        
        [self setDefaultImage:_currentImageIsMain ? _alternateImage.image : _mainImage.image ];
        _currentImageIsMain^=YES;
    }
}
- (void) onTapAction{
    if(_isTapped){
        if([self isHold]){
            [self fadeToUntapped];
            [self invokeOnTapRelease];
        }
    }else{
        _isTapped=YES;
        
        if(IS_FLASH_ON_TAP){
            [_toolbarController.mainViewController flash];
        }
        self.alpha = TAPPED_ALPHA;
        if(![self isHold]){
            [self fadeToUntapped];
        }
        [_toolbarController hideAllButtonsBut:self];
        [self invoke:_onTap named:@"onTap"];
    }
}

-(void) fadeToUntapped{
    if(self.alpha>UNTAPPED_ALPHA){
        
        [UIView animateWithDuration:FADE_TO_UNTAPPED_TIME animations:^{
            self.alpha = UNTAPPED_ALPHA;
        }];
    }
    
    _isTapped = NO;
}

-(void)showLibreOffice{
    [self setDefaultImage:_mainImage.image];
    _currentImageIsMain = YES;
    [self reset:UNTAPPED_ALPHA];
}

-(void)hideLibreOffice{
    [self reset: 0];
}

-(void) invoke:(MLOToolbarButtonCallback) callback named:(NSString *) name{
    callback();
    NSLog(@"%@ button perfromed: %@",_mainImage.name, name);
}

@end