1-------------------------------------------------------------------------------- 2-------------------------------------------------------------------------------- 3-- 4-- file: tweakmode.lua 5-- brief: provides default tweak mode call-ins for widgets 6-- author: Dave Rodgers 7-- 8-- Copyright (C) 2007. 9-- Licensed under the terms of the GNU GPL, v2 or later. 10-- 11-------------------------------------------------------------------------------- 12-------------------------------------------------------------------------------- 13 14-- 15-- This is a work in progress... 16-- 17 18include("keysym.h.lua") 19 20 21local xmin = 0 22local ymin = 0 23local xmax = 400 24local ymax = 400 25 26local xscale = 1 27local yscale = 1 28local xyscale = 1 29 30local xminOld = 0 31local yminOld = 0 32local xmaxOld = 0 33local ymaxOld = 0 34local xscaleOld = 1 35local yscaleOld = 1 36local xyscaleOld = 1 37 38local xminsize = 10 39local yminsize = 10 40 41local mouseScale = 1 42 43local UpdateHook = nil 44 45local xside = 0 46local yside = 0 47 48 49-------------------------------------------------------------------------------- 50-------------------------------------------------------------------------------- 51 52function InstallTweakMode(updateHook) 53 UpdateHook = updateHook 54end 55 56 57local function StartTweak() 58 xminOld = xmin 59 yminOld = ymin 60 xmaxOld = xmax 61 ymaxOld = ymax 62 xscaleOld = xscale 63 yscaleOld = yscale 64 xyscaleOld = xyscale 65end 66 67 68local function RevertTweak() 69 xmin = xminOld 70 xmax = xmaxOld 71 ymin = yminOld 72 ymax = ymaxOld 73 xscale = xscaleOld 74 yscale = yscaleOld 75 xyscale = xyscaleOld 76end 77 78 79local function SendUpdate() 80 81 if true then return end -- ??? 82 83 if (UpdateHook == nil) then 84 print('ERROR') 85 error('UpdateHook == nil, '..widget.whInfo.basename) 86 end 87 UpdateHook(xmin, ymin, xmax, ymax, xscale, yscale, xyscale) 88end 89 90 91local function IsAbove(x, y) 92 return ((x >= xmin) and (x <= xmax) and (y >= ymin) and (y <= ymax)) 93end 94 95 96-------------------------------------------------------------------------------- 97-------------------------------------------------------------------------------- 98 99function widget:TweakMousePress(x, y, button) 100 if (not IsAbove(x, y)) then 101 return false 102 end 103 local alt,ctrl,meta,shift = Spring.GetModKeyState() 104 if (ctrl) then 105 if (button == 1) then 106 widgetHandler:RaiseWidget() 107 elseif (button == 3) then 108 widgetHandler:LowerWidget() 109 end 110 end 111 StartTweak() 112 if (button == 3) then 113 local xsize = (xmax - xmin) 114 local ysize = (ymax - ymin) 115 local xmid = (xmax + xmin) * 0.5 116 local ymid = (ymax + ymin) * 0.5 117 if (x < (xmid - (0.15 * xsize))) then 118 xside = -1 119 elseif (x > (xmid + (0.15 * xsize))) then 120 xside = 1 121 else 122 xside = 0 123 end 124 if (y < (ymid - (0.15 * ysize))) then 125 yside = -1 126 elseif (y > (ymid + (0.15 * ysize))) then 127 yside = 1 128 else 129 yside = 0 130 end 131 end 132 return true 133end 134 135 136function widget:TweakMouseMove(x, y, dx, dy, button) 137-- print('TWEAK (TweakMouseMove) '..x..' '..y..' '..dx..' '..dy..' '..button) 138 if (not widgetHandler:IsMouseOwner()) then 139 return false 140 end 141 142 local vsx,vsy = widgetHandler:GetViewSizes() 143 if (button == 1) then 144 if ((xmin + dx) < 0) then dx = - xmin end 145 if ((ymin + dy) < 0) then dy = - ymin end 146 if ((xmax + dx) > vsx) then dx = vsx - xmax end 147 if ((ymax + dy) > vsy) then dy = vsy - ymax end 148 xmin = xmin + dx 149 xmax = xmax + dx 150 ymin = ymin + dy 151 ymax = ymax + dy 152 SendUpdate() 153 elseif (button == 3) then 154 if (xside == -1) then 155 xmin = xmin + dx 156 if (xmin < 0) then xmin = 0 end 157 if ((xmax - xmin) < xminsize) then xmin = xmax - xminsize end 158 elseif (xside == 1) then 159 xmax = xmax + dx 160 if (xmax > vsx) then xmax = vsx end 161 if ((xmax - xmin) < xminsize) then xmax = xmin + xminsize end 162 end 163 164 if (yside == -1) then 165 ymin = ymin + dy 166 if (ymin < 0) then ymin = 0 end 167 if ((ymax - ymin) < yminsize) then ymin = ymax - yminsize end 168 elseif (yside == 1) then 169 ymax = ymax + dy 170 if (ymax > vsy) then ymax = vsy end 171 if ((ymax - ymin) < yminsize) then ymax = ymin + yminsize end 172 end 173 SendUpdate() 174 end 175 return true 176end 177 178 179function widget:TweakMouseRelease(x, y, button) 180 print('TWEAK (TweakMouseRelease) '..x..' '..y..' '..button) 181 return -1 182end 183 184 185-------------------------------------------------------------------------------- 186-------------------------------------------------------------------------------- 187 188function widget:TweakIsAbove(x, y) 189 return IsAbove(x, y) 190end 191 192 193function widget:TweakGetTooltip(x, y) 194 return 'LMB = move\n'.. 195 'RMB = size\n'.. 196 'LMB+CTRL = raise\n'.. 197 'RMB+CTRL = lower' 198end 199 200 201-------------------------------------------------------------------------------- 202 203function widget:TweakKeyPress(key, mods, isRepeat) 204 print('TWEAK (TweakKeyPress) '..key) 205 return false 206end 207 208 209function widget:TweakKeyRelease(key, mods) 210 print('TWEAK (TweakKeyRelease) '..key) 211 if (key == KEYSYMS.ESCAPE) then 212 RevertTweak() 213 SendUpdate() 214 widgetHandler:DisownMouse() 215 return true 216 end 217 return false 218end 219 220 221-------------------------------------------------------------------------------- 222 223function widget:TweakDrawScreen() 224 local x,y = Spring.GetMouseState() 225 if (not widgetHandler:IsMouseOwner() and not IsAbove(x, y)) then 226 return 227 end 228 229 -- ??? add an indicator for xside/yside 230 231 gl.Blending(GL.SRC_ALPHA, GL.ONE) 232 gl.Color(0.8, 0.8, 1.0, 0.25) 233 gl.Shape(GL.QUADS, { 234 { v = { xmin, ymin } }, { v = { xmax, ymin } }, 235 { v = { xmax, ymax } }, { v = { xmin, ymax } } 236 }) 237 gl.Color(0.0, 0.0, 1.0, 0.5) 238 gl.Shape(GL.QUADS, { 239 { v = { xmin + 3, ymin + 3} }, { v = { xmax - 3, ymin + 3 } }, 240 { v = { xmax - 3, ymax - 3} }, { v = { xmin + 3, ymax - 3 } } 241 }) 242 gl.Color(1.0, 1.0, 1.0) 243 gl.Blending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA) 244end 245 246 247-------------------------------------------------------------------------------- 248