1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/ui/ui_check_box.h"
24 
25 #include "bladerunner/audio_player.h"
26 #include "bladerunner/bladerunner.h"
27 #include "bladerunner/game_constants.h"
28 #include "bladerunner/game_info.h"
29 #include "bladerunner/shape.h"
30 #include "bladerunner/time.h"
31 #include "bladerunner/ui/kia.h"
32 
33 namespace BladeRunner {
34 
UICheckBox(BladeRunnerEngine * vm,UIComponentCallback * valueChangedCallback,void * callbackData,Common::Rect rect,int style,bool isChecked)35 UICheckBox::UICheckBox(BladeRunnerEngine *vm, UIComponentCallback *valueChangedCallback, void *callbackData, Common::Rect rect, int style, bool isChecked)
36 	: UIComponent(vm) {
37 
38 	_valueChangedCallback = valueChangedCallback;
39 	_callbackData = callbackData;
40 
41 	_isEnabled = true;
42 	_hasFocus = false;
43 	_isPressed = false;
44 	_style = style;
45 
46 	if (isChecked) {
47 		_frame = 5u;
48 	} else {
49 		_frame = 0u;
50 	}
51 
52 	_timeLast = _vm->_time->currentSystem();
53 	_rect = rect;
54 	_isChecked = isChecked;
55 }
56 
57 
draw(Graphics::Surface & surface)58 void UICheckBox::draw(Graphics::Surface &surface) {
59 	if (_rect.right > _rect.left && _rect.bottom > _rect.top) {
60 		uint32 shapeId;
61 
62 		uint32 timeNow = _vm->_time->currentSystem();
63 		// unsigned difference is intentional
64 		if (timeNow - _timeLast > 67u) {
65 			// unsigned difference is intentional
66 			uint32 frameDelta = (timeNow - _timeLast) / 67u;
67 			_timeLast = timeNow;
68 
69 			if (_isChecked) {
70 				_frame = MIN<uint32>(_frame + frameDelta, 5u);
71 			} else {
72 				_frame = (_frame < frameDelta) ? 0 : MAX<uint32>(_frame - frameDelta, 0u);
73 			}
74 		}
75 
76 		if (_style) {
77 			if (_frame || (_hasFocus && !_isPressed && _isEnabled)) {
78 				if (_frame != 5u || (_hasFocus && !_isPressed && _isEnabled)) {
79 					shapeId = _frame + 54u;
80 				} else {
81 					shapeId = 53u;
82 				}
83 			} else {
84 				shapeId = 52u;
85 			}
86 		} else {
87 			if (_frame || (_hasFocus && !_isPressed && _isEnabled)) {
88 				if (_frame != 5u || (_hasFocus && !_isPressed && _isEnabled)) {
89 					shapeId = _frame + 62u;
90 				} else {
91 					shapeId = 61u;
92 				}
93 			} else {
94 				shapeId = 60u;
95 			}
96 		}
97 
98 		_vm->_kia->_shapes->get(shapeId)->draw(surface, _rect.left, _rect.top + 1);
99 	}
100 }
101 
enable()102 void UICheckBox::enable() {
103 	_isEnabled = true;
104 	_isPressed = false;
105 	_hasFocus = false;
106 }
107 
disable()108 void UICheckBox::disable() {
109 	_isEnabled = false;
110 }
111 
setChecked(bool isChecked)112 void UICheckBox::setChecked(bool isChecked) {
113 	_isChecked = isChecked;
114 }
115 
handleMouseMove(int mouseX,int mouseY)116 void UICheckBox::handleMouseMove(int mouseX, int mouseY) {
117 	if (_rect.contains(mouseX, mouseY)) {
118 		if (!_hasFocus && _isEnabled && !_isPressed ) {
119 			_vm->_audioPlayer->playAud(_vm->_gameInfo->getSfxTrack(kSfxTEXT3), 100, 0, 0, 50, 0);
120 		}
121 		_hasFocus = true;
122 	} else {
123 		_hasFocus = false;
124 	}
125 }
126 
handleMouseDown(bool alternateButton)127 void UICheckBox::handleMouseDown(bool alternateButton) {
128 	if (!alternateButton) {
129 		if (_isEnabled && _hasFocus) {
130 			_isChecked = !_isChecked;
131 			if (_valueChangedCallback) {
132 				_valueChangedCallback(_callbackData, this);
133 			}
134 			_vm->_audioPlayer->playAud(_vm->_gameInfo->getSfxTrack(kSfxBEEP10), 100, 0, 0, 50, 0);
135 		} else {
136 			_isPressed = true;
137 		}
138 	}
139 }
140 
handleMouseUp(bool alternateButton)141 void UICheckBox::handleMouseUp(bool alternateButton) {
142 	_isPressed = false;
143 }
144 
145 } // End of namespace BladeRunner
146