-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiscroll.mm
221 lines (185 loc) · 5.4 KB
/
multiscroll.mm
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// twbt.cpp
// twbt
//
// Created by Vitaly Pronkin on 14/05/14.
// Copyright (c) 2014 mifki. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
#include <stdint.h>
#include <iostream>
#include <map>
#include <vector>
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/Maps.h"
#include "modules/World.h"
#include "modules/MapCache.h"
#include "modules/Gui.h"
#include "modules/Screen.h"
#include "df/world.h"
#include "df/construction.h"
#include "df/graphic.h"
#include "df/enabler.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/renderer.h"
#include "renderer_twbt.h"
using namespace DFHack;
using df::global::world;
using std::string;
using std::vector;
using df::global::enabler;
using df::global::gps;
using df::global::ui;
using df::global::window_x;
using df::global::window_y;
DFHACK_PLUGIN("multiscroll");
CFMachPortRef etap;
CFRunLoopSourceRef esrc;
color_ostream *out2;
CGEventRef MyEventTapCallBack (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
if ((int)type < 0)
return NULL;
if (type != kCGEventScrollWheel)
return event;
df::viewscreen * ws = Gui::getCurViewscreen();
if (!strict_virtual_cast<df::viewscreen_dwarfmodest>(ws))
return event;
static float accdx = 0, accdy = 0;
NSEvent *e = [NSEvent eventWithCGEvent:event];
static bool zooming;
if (e.phase == NSEventPhaseBegan || e.momentumPhase == NSEventPhaseBegan)
{
accdx = accdy = 0;
zooming = ([NSEvent modifierFlags] & NSCommandKeyMask);
}
if (zooming && !([NSEvent modifierFlags] & NSCommandKeyMask))
return NULL;
accdx += [e scrollingDeltaX];
accdy += [e scrollingDeltaY];
renderer_cool *r = (renderer_cool*)enabler->renderer;
bool nextgen = r->is_twbt();
/*int dispx, dispy;
if (nextgen)
{
dispx = r->gdispx;
dispy = r->gdispy;
}
else
{
dispx = r->dispx;
dispy = r->dispy;
}*/
int dx = accdx / 16;
int dy = accdy / 16;
if (nextgen && zooming)
{
if (dy > 0)
{
accdy -= dy*16;
r->needs_zoom = 1;
r->needs_reshape = true;
}
if (dy < 0)
{
accdx -= dx*16;
accdy -= dy*16;
renderer_cool *r = (renderer_cool*)enabler->renderer;
r->needs_zoom = -1;
r->needs_reshape = true;
}
return NULL;
}
if (dx || dy)
{
accdx -= dx*16;
accdy -= dy*16;
*window_x -= dx;
*window_y -= dy;
int mx = world->map.x_count;
int my = world->map.y_count;
int w = nextgen ? r->gdimxfull : gps->dimx;
int h = nextgen ? r->gdimyfull : gps->dimy;
if (dx < 0) //map moves to the left
{
int addw;
if (nextgen)
addw = 0;
else
{
int sidewidth;
uint8_t menu_width, area_map_width;
Gui::getMenuWidth(menu_width, area_map_width);
bool menuforced = (ui->main.mode != df::ui_sidebar_mode::Default || df::global::cursor->x != -30000);
if ((menuforced || menu_width == 1) && area_map_width == 2) // Menu + area map
sidewidth = 55;
else if (menu_width == 2 && area_map_width == 2) // Area map only
sidewidth = 24;
else if (menu_width == 1) // Wide menu
sidewidth = 55;
else if (menuforced || (menu_width == 2 && area_map_width == 3)) // Menu only
sidewidth = 31;
else
sidewidth = 0;
addw = sidewidth + 2;
}
if (mx > w - addw)
{
if (*window_x > mx - w + addw)
*window_x = mx - w + addw;
}
else
*window_x = 0;
}
else if (*window_x <= 0)
*window_x = 0;
int addh = nextgen ? 0 : 2;
if (my > h - addh)
{
if (*window_y <= 0)
*window_y = 0;
else if (*window_y > my - h + addh)
*window_y = my - h + addh;
}
else
*window_y = 0;
}
return NULL;
}
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{
if (!enabler->renderer->uses_opengl())
{
out.color(COLOR_RED);
out << "MultiScroll: OpenGL renderer is required" << std::endl;
out.color(COLOR_RESET);
return CR_OK;
}
out2 = &out;
ProcessSerialNumber curPSN;
GetCurrentProcess(&curPSN);
etap = CGEventTapCreateForPSN(&curPSN, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMaskBit(NX_SCROLLWHEELMOVED), MyEventTapCallBack, NULL);
if (!etap)
return CR_FAILURE;
esrc = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, etap, 0);
if (!esrc)
{
CFRelease(etap);
return CR_FAILURE;
}
CFRunLoopAddSource(CFRunLoopGetMain(), esrc, kCFRunLoopCommonModes);
CGEventTapEnable(etap, 1);
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
CGEventTapEnable(etap, 0);
CFRunLoopRemoveSource(CFRunLoopGetMain(), esrc, kCFRunLoopCommonModes);
CFRelease(esrc);
CFRelease(etap);
return CR_OK;
}