forked from jankowskib/D2Ex2
/
ExScrollBar.cpp
157 lines (139 loc) · 4.65 KB
/
ExScrollBar.cpp
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
/*==========================================================
* D2Ex2
* https://github.com/lolet/D2Ex2
* ==========================================================
* Copyright (c) 2011-2014 Bartosz Jankowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==========================================================
*/
#include "stdafx.h"
#include "ExScrollBar.h"
#include "Misc.h"
void Increase(ExControl*tt){
ASSERT(tt->ptParent);
ExScrollBar* Parent = (ExScrollBar*)tt->ptParent;
if (*Parent->sValue<Parent->sMax) {
*Parent->sValue = (*Parent->sValue) + 1;
DEBUGMSG("sVal %d, sMax %d, sMin %d", *Parent->sValue, Parent->sMax, Parent->sMin);
Parent->Update();
}
}
void Decrease(ExControl*tt) {
ASSERT(tt->ptParent);
ExScrollBar* Parent = (ExScrollBar*)tt->ptParent;
if (*Parent->sValue>Parent->sMin) {
*Parent->sValue = (*Parent->sValue) - 1;
DEBUGMSG("sVal %d, sMax %d, sMin %d", *Parent->sValue, Parent->sMax, Parent->sMin);
Parent->Update();
}
}
ExScrollBar::ExScrollBar(int X, int Y, int Min, int Max, int Height, int * Var, void(*efnOnChange)(ExControl*)) : ExControl(X, Y, 13, Height, 0)
{
try
{
aCellFile = new ExCellFile(CellFiles::SCROLLBAR);
}
catch (const CellLoadError&)
{
delete aCellFile;
LeaveCriticalSection(&CON_CRITSECT);
D2EXERROR("Cannot create a scroll bar because of missing or corrupted DC6 file!")
}
aCellFile->SetFrame(2);
sMin = Min;
sMax = Max;
sValue = Var;
fnOnChange = efnOnChange;
DontEnterCS = true;
DontLeaveCS = true;
Slider = new ExRectangle(cX + 2, cY + 12, 9, Height - 24, 0x0C, false);
Up = new ExButton(cX, cY + 11, 0, 5, L"", CellFiles::SCROLLBAR, &Decrease, false);
DontEnterCS = true;
Down = new ExButton(cX, cY + Height, 0, 0, L"", CellFiles::SCROLLBAR, &Increase, false);
DontLeaveCS = false;
DontEnterCS = false;
if (Max - Min == 0) Slider->cState = INVISIBLE;
if (*sValue > Max) *sValue = Max;
if (*sValue < Min) *sValue = Min;
Slider->ptParent = Down->ptParent = Up->ptParent = this;
int c = sMax - sMin + 1;
float ts = (float)(GetHeight() - 24) / c;
int o = Misc::round((*sValue) * ts);
DEBUGMSG("c = %d, ts =%f, o = %d, %d", c, ts, o, GetHeight());
Slider->SetY(cY + 12 + o);
Slider->SetHeight(Misc::round(ts) + 5);
LeaveCriticalSection(&CON_CRITSECT);
}
void ExScrollBar::Update()
{
int c = sMax - sMin + 1;
float ts = (float)(GetHeight() - 24) / c;
int o = Misc::round((*sValue) * ts);
DEBUGMSG("c = %d, ts =%f, o = %d, %d", c, ts, o, GetHeight());
Slider->SetY(cY + 12 + o);
Slider->SetHeight(Misc::round(ts) + 5);
if (fnOnChange) fnOnChange(this);
}
void ExScrollBar::SetHeight(int cH)
{
cHeight = cH;
Down->SetY(cY + cHeight);
}
void ExScrollBar::Draw()
{
if (cState == INVISIBLE) return;
for (int ty = cY + 22; ty < cY + cHeight; ty += 12)
{
D2Funcs.D2GFX_DrawCellContextEx(aCellFile->Get(), cX, ty, -1, 5, 0);
}
}
void ExScrollBar::SetState(States aState)
{
cState = aState;
Up->SetState(aState);
Down->SetState(aState);
Slider->SetState(aState);
}
bool ExScrollBar::isPressed(unsigned int Sender, WPARAM wParam)
{
if (cState == INVISIBLE) return false;
switch (Sender) {
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_UP: {if (*sValue > sMin) { *sValue = *sValue - 1; Update(); } return true; }
case VK_DOWN: {if (*sValue<sMax) { *sValue = *sValue + 1; Update(); } return true; }
case VK_PRIOR:{if (*sValue >= sMin + 5) { *sValue = *sValue - 5; Update(); } else if (*sValue>sMin) { *sValue = sMin; Update(); } return true; } /*PGUP*/
case VK_NEXT: {if (*sValue <= sMax - 5) { *sValue = *sValue + 5; Update(); } else if (*sValue<sMax) { *sValue = sMax; Update(); } return true; } /*PGDN*/
case VK_HOME: {if (*sValue>sMin) { *sValue = sMin; Update(); } return true; }
case VK_END: {if (*sValue<sMax) { *sValue = sMax; Update(); } return true; }
}
}
case WM_MOUSEWHEEL:
{
if (GET_WHEEL_DELTA_WPARAM(wParam)>0) { if (*sValue > sMin) { *sValue = *sValue - 1; Update(); } return true; }
else if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) { if (*sValue < sMax) { *sValue = *sValue + 1; Update(); } return true; }
}
}
return false;
}
ExScrollBar::~ExScrollBar()
{
delete Slider;
delete Up;
delete Down;
delete aCellFile;
EnterCriticalSection(&CON_CRITSECT);
}