diff options
Diffstat (limited to 'xmake/core/ui/rect.lua')
| -rw-r--r-- | xmake/core/ui/rect.lua | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/xmake/core/ui/rect.lua b/xmake/core/ui/rect.lua new file mode 100644 index 000000000..6264ad67c --- /dev/null +++ b/xmake/core/ui/rect.lua @@ -0,0 +1,142 @@ + +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file rect.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: rect.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local point = require("ui/point") +local object = require("ui/object") + +-- define module +local rect = rect or object { _init = {"sx", "sy", "ex", "ey"} } + +-- get rect size +function rect:size() + return point { self.ex - self.sx, self.ey - self.sy } +end + +-- move rect +function rect:move(dx, dy) + self.sx = self.sx + dx + self.sy = self.sy + dy + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- move top right corner of the rect +function rect:moves(dx, dy) + self.sx = self.sx + dx + self.sy = self.sy + dy + return self +end + +-- move bottom left corner of the rect +function rect:movee(dx, dy) + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- expand rect area +function rect:grow(dx, dy) + self.sx = self.sx - dx + self.sy = self.sy - dy + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- set rect with shared area between this rect and a given rect +function rect:intersect(r) + self.sx = math.max(self.sx, r.sx) + self.sy = math.max(self.sy, r.sy) + self.ex = math.min(self.ex, r.ex) + self.ey = math.min(self.ey, r.ey) + return self +end + +-- get rect with shared area between two rects: local rect_new = r1 / r2 +function rect:__div(r) + return self():intersect(r) +end + +-- set union rect +function rect:union(r) + self.sx = math.min(self.sx, r.sx) + self.sy = math.min(self.sy, r.sy) + self.ex = math.max(self.ex, r.ex) + self.ey = math.max(self.ey, r.ey) + return self +end + +-- get union rect: local rect_new = r1 + r2 +function rect:__add(r) + return self():union(r) +end + +-- r1 == r1? +function rect:__eq(r) + return + self.sx == r.sx and + self.sy == r.sy and + self.ex == r.ex and + self.ey == r.ey +end + +-- contains the given point in rect? +function rect:contains(x, y) + return x >= self.sx and x < self.ex and y >= self.sy and y < self.ey +end + +-- empty rect? +function rect:empty() + return self.sx >= self.ex or self.sy >= self.ey +end + +-- tostring(r) +function rect:__tostring() + if self:empty() then + return '[empty]' + end + return '[(' .. self.sx .. ',' .. self.sy .. '),(' .. self.ex .. ',' .. self.ey .. ')]' +end + +-- r1 .. r2 +function rect.__concat(op1, op2) + if type(op1) == 'string' then + return op1 .. op2:__tostring() + elseif type(op2) == 'string' then + return op1:__tostring() .. op2 + else + return op1:__tostring() .. op2:__tostring() + end +end + +-- return module +return rect |
