File: tweakmode.lua

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (247 lines) | stat: -rw-r--r-- 5,980 bytes parent folder | download | duplicates (8)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    tweakmode.lua
--  brief:   provides default tweak mode call-ins for widgets
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

--
--  This is a work in progress...
--

include("keysym.h.lua")


local xmin = 0
local ymin = 0
local xmax = 400
local ymax = 400

local xscale = 1
local yscale = 1
local xyscale = 1

local xminOld = 0
local yminOld = 0
local xmaxOld = 0
local ymaxOld = 0
local xscaleOld = 1
local yscaleOld = 1
local xyscaleOld = 1

local xminsize = 10
local yminsize = 10

local mouseScale = 1

local UpdateHook = nil

local xside = 0
local yside = 0


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function InstallTweakMode(updateHook)
  UpdateHook = updateHook
end


local function StartTweak()
  xminOld    = xmin
  yminOld    = ymin
  xmaxOld    = xmax
  ymaxOld    = ymax
  xscaleOld  = xscale
  yscaleOld  = yscale
  xyscaleOld = xyscale
end


local function RevertTweak()
  xmin    = xminOld
  xmax    = xmaxOld
  ymin    = yminOld
  ymax    = ymaxOld
  xscale  = xscaleOld
  yscale  = yscaleOld
  xyscale = xyscaleOld
end


local function SendUpdate()
  
  if true then return end -- ???
  
  if (UpdateHook == nil) then
    print('ERROR')
    error('UpdateHook == nil, '..widget.whInfo.basename)
  end
  UpdateHook(xmin, ymin, xmax, ymax, xscale, yscale, xyscale)
end


local function IsAbove(x, y)
  return ((x >= xmin) and (x <= xmax) and (y >= ymin) and (y <= ymax))
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:TweakMousePress(x, y, button)
  if (not IsAbove(x, y)) then
    return false
  end
  local alt,ctrl,meta,shift = Spring.GetModKeyState()
  if (ctrl) then
    if (button == 1) then
      widgetHandler:RaiseWidget()
    elseif (button == 3) then
      widgetHandler:LowerWidget()
    end
  end
  StartTweak()
  if (button == 3) then
    local xsize = (xmax - xmin)
    local ysize = (ymax - ymin)
    local xmid  = (xmax + xmin) * 0.5
    local ymid  = (ymax + ymin) * 0.5
        if (x < (xmid - (0.15 * xsize))) then
      xside = -1
    elseif (x > (xmid + (0.15 * xsize))) then
      xside = 1
    else
      xside = 0
    end
        if (y < (ymid - (0.15 * ysize))) then
      yside = -1
    elseif (y > (ymid + (0.15 * ysize))) then
      yside = 1
    else
      yside = 0
    end
  end
  return true
end


function widget:TweakMouseMove(x, y, dx, dy, button)
--  print('TWEAK (TweakMouseMove) '..x..' '..y..' '..dx..' '..dy..' '..button)
  if (not widgetHandler:IsMouseOwner()) then
    return false
  end
  
  local vsx,vsy = widgetHandler:GetViewSizes()
  if (button == 1) then
    if ((xmin + dx) < 0) then dx = - xmin end
    if ((ymin + dy) < 0) then dy = - ymin end
    if ((xmax + dx) > vsx) then dx = vsx - xmax end
    if ((ymax + dy) > vsy) then dy = vsy - ymax end
    xmin = xmin + dx
    xmax = xmax + dx
    ymin = ymin + dy
    ymax = ymax + dy
    SendUpdate()
  elseif (button == 3) then
    if (xside == -1) then
      xmin = xmin + dx
      if (xmin < 0) then xmin = 0 end
      if ((xmax - xmin) < xminsize) then xmin = xmax - xminsize end
    elseif (xside == 1) then
      xmax = xmax + dx
      if (xmax > vsx) then xmax = vsx end
      if ((xmax - xmin) < xminsize) then xmax = xmin + xminsize end
    end

    if (yside == -1) then
      ymin = ymin + dy
      if (ymin < 0) then ymin = 0 end
      if ((ymax - ymin) < yminsize) then ymin = ymax - yminsize end
    elseif (yside == 1) then
      ymax = ymax + dy
      if (ymax > vsy) then ymax = vsy end
      if ((ymax - ymin) < yminsize) then ymax = ymin + yminsize end
    end
    SendUpdate()
  end
  return true
end


function widget:TweakMouseRelease(x, y, button)
  print('TWEAK (TweakMouseRelease) '..x..' '..y..' '..button)
  return -1
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:TweakIsAbove(x, y)
  return IsAbove(x, y)
end


function widget:TweakGetTooltip(x, y)
  return 'LMB = move\n'..
         'RMB = size\n'..
         'LMB+CTRL = raise\n'..
         'RMB+CTRL = lower'
end


--------------------------------------------------------------------------------

function widget:TweakKeyPress(key, mods, isRepeat)
  print('TWEAK (TweakKeyPress) '..key)
  return false
end


function widget:TweakKeyRelease(key, mods)
  print('TWEAK (TweakKeyRelease) '..key)
  if (key == KEYSYMS.ESCAPE) then
    RevertTweak()
    SendUpdate()
    widgetHandler:DisownMouse()
    return true
  end
  return false
end


--------------------------------------------------------------------------------

function widget:TweakDrawScreen()
  local x,y = Spring.GetMouseState()
  if (not widgetHandler:IsMouseOwner() and not IsAbove(x, y)) then
    return
  end
  
  -- ??? add an indicator for xside/yside
  
  gl.Blending(GL.SRC_ALPHA, GL.ONE)
  gl.Color(0.8, 0.8, 1.0, 0.25)
  gl.Shape(GL.QUADS, {
    { v = { xmin, ymin } }, { v = { xmax, ymin } }, 
    { v = { xmax, ymax } }, { v = { xmin, ymax } }
  })
  gl.Color(0.0, 0.0, 1.0, 0.5)
  gl.Shape(GL.QUADS, {
    { v = { xmin + 3, ymin + 3} }, { v = { xmax - 3, ymin + 3 } }, 
    { v = { xmax - 3, ymax - 3} }, { v = { xmin + 3, ymax - 3 } }
  })
  gl.Color(1.0, 1.0, 1.0)
  gl.Blending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
end


--------------------------------------------------------------------------------