All pastes #904440 Raw Edit

window.lua

public lua v1 · immutable
·published 2008-02-14 22:59 UTC
rendered paste body
--[[ Base window class ]]--window = {}function window.new(options)  options = options or {}  local self = setmetatable({}, {__index = options.class or window})  local parent = nil    self.class = window  self.width = 100  self.height = 100  self.left = 500  self.top = 600  self.text = "Europe in Ruins"  self.z = 0  self.children = {}  for key, value in pairs(options) do    self[key] = value  end  if self.parent then    parent = self.parent    self.parent = nil  end    self:loadGfx()  self:loadFont()  self:loadRegion()  self:onResize()    if parent then    parent:addChild(self)  end    return selfendfunction window:move(x, y)  self.left, self.top = x,y  self:onMove()endfunction window:resize(width, height)  self.width, self.height = width, height  self:onResize()endfunction window:setZ(z)  self.z = z  if self.parent then    self.parent:sortChildren()  endendfunction window:loadRegion()  local function onUp()      self.mouse_region:flags("Relative", "Clip")      self.mouse_region:setOnMove(nil)      self.mouse_region:setOnUp(nil)    end  local function onDown(X, Y)    if Y < self.images[1][2]:getHeight() then      local function onMove(x, y)        self:move(x - X, y - Y)      end      self.mouse_region:flags("Absolute", "NoClip")      self.mouse_region:setOnMove(onMove)      self.mouse_region:setOnUp(onUp)    end  end  self.mouse_region = spooge.region(self.left, self.top, self.width, self.height)  self.mouse_region:setOnDown(onDown)endfunction window:updateRegion()  self.mouse_region:position(self.left, self.top)  self.mouse_region:size(self.width, self.height)endfunction window:onChangeParent(newparent)  self.mouse_region:parent(newparent and newparent.mouse_region)endfunction window:loadFont()  if not rawget(self.class, "font") then    self.class.font = spooge.font{face = "Arial", height = 15}    self.class.font:flags("Left", "NoClip", "SingleLine", "VCenter")  end  self.text_left = self.text_left or self.class.images[1][1]:getWidth () + 4  self.text_height = self.text_height or self.class.images[1][2]:getHeight()  self.text_top = self.text_top or 0endfunction window:loadGfx()  if not rawget(self.class, "images") then    self.class.images = {}    self.images_folder = self.images_folder or [[EuropeInRuins\\UI\\]]    print(self.images_prefix)    self.images_prefix = self.images_prefix or [[window_]]    print(self.images_prefix)    local prefix = self.images_folder .. self.images_prefix    local rows = {"top" , "", "bottom"}    local cols = {"left", "", "right" }    for row = 1, #rows do      self.class.images[row] = {}      for col = 1, #cols do        self.class.images[row][col] = spooge.image(prefix .. rows[row] .. cols[col] .. ".png")      end    end  endendfunction window:onMove()  self:updateRegion()  if self.children and #self.children > 0 then    for _, child in ipairs(self.children) do      child:onParentMove(1)    end  endendfunction window:onParentMove(level)  if self.children and #self.children > 0 then    local nextlevel = level + 1    for _, child in ipairs(self.children) do      child:onParentMove(nextlevel)    end  endendfunction window:onResize()  self.images = nil  local raw_images = self.images  self.images = {}  for rid, row in ipairs(raw_images) do    self.images[rid] = {}    for cid, img in ipairs(row) do      self.images[rid][cid] = img:copy()    end  end    local colwidth   = {self.images[2][1]:getWidth (), 0, self.images[2][3]:getWidth ()}  local rowheight  = {self.images[1][2]:getHeight(), 0, self.images[3][2]:getHeight()}  colwidth [2] = self.width  - colwidth [1] - colwidth [3]  rowheight[2] = self.height - rowheight[1] - rowheight[3]    local y = 0  for rid, row in ipairs(self.images) do    local x = 0    for cid, img in ipairs(row) do      img        :resize(colwidth[cid], rowheight[rid])        :translate(x, y, true)      x = x + colwidth[cid]    end    y = y + rowheight[rid]  end    self:updateRegion()endfunction window:render(left, top)  left, top = (left or 0) + self.left, (top or 0) + self.top  for rid, row in ipairs(self.images) do    for cid, img in ipairs(row) do      img:translate(left, top, true)      img:render(0, 0)      img:translate(-left, -top, true)    end  end  self.font:render(self.text, left + self.text_left, top + self.text_top, 0, self.text_height)  self:renderChildren(left, top)endfunction window:addChild(child)  child:onChangeParent(self)  if child.parent then    if child.parent == self then      return    end    child.parent:removeChild(child)  end  self.children = self.children or {}  self.children[#self.children + 1] = child  child.parent = self  self:sortChildren()  return childendfunction window:sortChildren()  table.sort(self.children, function(c1, c2) return c1.z < c2.z end)endfunction window:renderChildren(left, top)  if self.children and #self.children > 0 then    for _, child in ipairs(self.children) do      child:render(left, top)    end  endend--[[ Button class ]]--button = setmetatable({}, {__index = window})button.images_prefix = [[button_]]button.text_left = 10function button.new(options)  options = options or {}  options.class = options.class or button  options.width = options.width or 128  options.height = options.height or 24  options.text = options.text or "button"  return window.new(options)endfunction button:loadGfx()  if not rawget(self.class, "imagesact") then    local cimages, prefix = rawget(self.class, "images"), self.images_prefix    self.class.images = nil    self.images_prefix = [[buttonact_]]    window.loadGfx(self)    self.class.imagesact = self.class.images    self.images_prefix = prefix    self.class.images = cimages  end  return window.loadGfx(self)endfunction button:loadRegion()  local function depress(de)    self.depressed = de    local imageDataSource = self.class.images    if de then      self.text_left = self.text_left + 2      self.text_top = 2      self.text_height = self.text_height - self.text_top      imageDataSource = self.class.imagesact    else      self.text_height = self.text_height + self.text_top      self.text_top = 0      self.text_left = nil    end    for rid, row in ipairs(self.images) do      for cid, img in ipairs(row) do        img:imageData(imageDataSource[rid][cid])      end    end  end  local function onDown()    self.awaiting_up = true    depress(true)  end  local function onUp()    if self.depressed and self.click then      self.click()    end    self.awaiting_up = nil    depress(nil)  end  local function onEnter()    if not rawget(self.class, "font_hover") then      self.class.font_hover = self.class.font:copy()      self.class.font_hover:colour(196, 181, 80)    end    if self.awaiting_up then      depress(true)    end    self.font = self.class.font_hover  end  local function onLeave()    if self.awaiting_up then      depress(nil)    end    self.font = nil  end  self.mouse_region = spooge.region(self.left, self.top, self.width, self.height)  self.mouse_region:setOnDown(onDown)  self.mouse_region:setOnUp(onUp)  self.mouse_region:setOnEnter(onEnter)  self.mouse_region:setOnLeave(onLeave)endfunction button:onResize()  self.text_height = self.height  return window.onResize(self)end--[[ Static image class ]]--static_image = setmetatable({}, {__index = window})--[[ Static text class ]]--static_text = setmetatable({}, {__index = window})--[[ Special desktop class ]]--local desktop = setmetatable({}, {__index = window})function desktop:loadGfx() endfunction desktop:loadFont() endfunction desktop:loadRegion() endfunction desktop:updateRegion() endfunction desktop:onResize() endfunction desktop:render()  self:renderChildren(0, 0)end--[[ Register the overlay ]]--OVERLAY = window.new{width = -1, height = -1, class = desktop}spooge.setOnBeforeEndScene(function()  OVERLAY:render()end)