查看: 3014|回复: 3

[随意闲聊] 【卖萌第四弹】一起来写UI吧

[复制链接]
梦石
0
星屑
76
在线时间
1379 小时
注册时间
2012-7-5
回帖
1643

开拓者

发表于 2015-1-1 16:30:44 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
0.gif
(不是指MVC之类完整的"UI"概念, 只是按内建Window类的原理创建任意大小的窗口背景图, 还请各位大触不要在UI概念上触窝)
正确的食用姿势[pre lang="ruby" line="1"]def Color.sample(alpha=255)
  Color.new([0,120,255][rand(3)],[0,120,255][rand(3)],[0,120,255][rand(3)],alpha)
end
def sp(*args)
  s = Sprite.new
  b = Bitmap.new(*args)
  yield b if block_given?
  s.bitmap = b
  return s
end
def rgss_run(*objs)
  loop do
    Graphics.update
    Input.update
    objs.each(&:update)
  end
end
class M_M
  attr_accessor :x, :y, :width, :height
  def initialize
    @hash = get_pieces(Bitmap.new("Resources/title_line.png"),12,198,34,56)
  end
  def update
    update_positions
    if Input.trigger?(Input::C)
      @x = rand 320
      @y = rand 240
      @width = rand 480
      @height = rand 320
    end
  end
  def update_positions
    w0 = @width-@hash[:corner0].bitmap.width-@hash[:corner1].bitmap.width
    h0 = @height-@hash[:corner0].bitmap.height-@hash[:corner2].bitmap.height
    @hash[:corner0].x += dynamic_distance(@x, @hash[:corner0].x, 7)
    @hash[:corner0].y += dynamic_distance(@y, @hash[:corner0].y, 7)
    @hash[:side0].src_rect.width += dynamic_distance(
      w0,@hash[:side0].src_rect.width,7)
    @hash[:side1].src_rect.height += dynamic_distance(
      h0,@hash[:side1].src_rect.height,7)
    @hash[:side2].src_rect.height += dynamic_distance(
      h0,@hash[:side2].src_rect.height,7)
    @hash[:side3].src_rect.width += dynamic_distance(
      w0,@hash[:side3].src_rect.width,7)
    @hash[:back].src_rect.width += dynamic_distance(
      w0,@hash[:back].src_rect.width,7)
    @hash[:back].src_rect.height += dynamic_distance(
      h0,@hash[:back].src_rect.height,7)
    w1 = @hash[:corner0].bitmap.width
    w2 = @hash[:side0].src_rect.width+w1
    h1 = @hash[:corner0].bitmap.height
    h2 = @hash[:side1].src_rect.height+h1
    @hash[:corner1].x = @hash[:corner0].x + w2
    @hash[:corner2].x = @hash[:corner0].x
    @hash[:corner3].x = @hash[:corner0].x + w2
    @hash[:corner1].y = @hash[:corner0].y
    @hash[:corner2].y = @hash[:corner0].y + h2
    @hash[:corner3].y = @hash[:corner0].y + h2
    @hash[:side0].x = @hash[:corner0].x + w1
    @hash[:side1].x = @hash[:corner0].x
    @hash[:side2].x = @hash[:corner0].x + w2
    @hash[:side3].x = @hash[:corner0].x + w1
    @hash[:side0].y = @hash[:corner0].y
    @hash[:side1].y = @hash[:corner0].y + h1
    @hash[:side2].y = @hash[:corner0].y + h1
    @hash[:side3].y = @hash[:corner0].y + h2
    @hash[:back].x = @hash[:corner0].x + w1
    @hash[:back].y = @hash[:corner0].y + h1
  end
  def set(x, y, w, h)
    @x, @y, @width, @height = x, y, w, h
  end
  def get_pieces(bitmap, x1, x2, y1, y2)
    w, h = bitmap.width, bitmap.height
    return {
      :back    => miao(bitmap, Rect.new(x1,y1,x2-x1,y2-y1), 640, 480), # 中间
      :corner0 => miao(bitmap, Rect.new( 0, 0,   x1,   y1),  x1,  y1), # 左上
      :corner1 => miao(bitmap, Rect.new(x2, 0, w-x2,   y1),w-x2,  y1), # 右上
      :corner2 => miao(bitmap, Rect.new( 0,y2,   x1, h-y2),  x1,h-y2), # 左下
      :corner3 => miao(bitmap, Rect.new(x2,y2, w-x2, h-y2),w-x2,h-y2), # 右下
      :side0   => miao(bitmap, Rect.new(x1, 0,x2-x1,   y1), 640,  y1), # 上部
      :side1   => miao(bitmap, Rect.new( 0,y1,   x1,y2-y1),  x1, 480), # 左部
      :side2   => miao(bitmap, Rect.new(x2,y1, w-x2,y2-y1),w-x2, 480), # 右部
      :side3   => miao(bitmap, Rect.new(x1,y2,x2-x1, h-y2), 640,h-y2), # 下部
    }
  end
  def miao(*args)
    return new_sprite(pick_bitmap(*args))
  end
  def new_sprite(bitmap)
    s = Sprite.new
    s.bitmap = bitmap
    s.z = 2333333
    return s
  end
  def pick_bitmap(bitmap, src_rect, w, h)
    b = Bitmap.new w, h
    b.stretch_blt b.rect, bitmap, src_rect
    return b
  end
  # a 目标值 b 基础值 c 速度的倒数【啥
  def dynamic_distance(a, b, c)
    (a - b).abs < c ? (a - b) == 0 ? 0 : (a - b).abs / (a - b) : (a - b) / c
  end
end

m = M_M.new
m.x = m.y = m.width = m.height = 200
sp(640, 480){|b| b.fill_rect b.rect, Color.sample }

rgss_run m[/pre]
资源:
title_line.png


喵喵喵~

评分

参与人数 1星屑 +90 收起 理由
进击の虎叔 + 90

查看全部评分


  -fk: -azogi:
梦石
0
星屑
60
在线时间
936 小时
注册时间
2008-2-14
回帖
930

开拓者

发表于 2015-1-1 16:47:14 | 显示全部楼层
滑动的效果非常流畅也很自然~看起来很舒服

评分

参与人数 1星屑 +2 收起 理由
kuerlulu + 2 断电阿姨喵~

查看全部评分

接稿,UI(已接)/立绘(已接)
无偿/有偿皆可,有偿速度会更快^q^
作品请见相册,有意请发私信:)
回复

使用道具 举报

梦石
0
星屑
9292
在线时间
2504 小时
注册时间
2011-5-20
回帖
14521

开拓者

发表于 2015-1-1 16:52:21 | 显示全部楼层
不知道颜色能不能直接用脚本改···

点评

噗  发表于 2015-1-1 16:54
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复

使用道具 举报

…あたしは天

梦石
0
星屑
2308
在线时间
4033 小时
注册时间
2010-10-4
回帖
10548

开拓者贵宾

发表于 2015-1-1 16:55:04 | 显示全部楼层
本帖最后由 taroxd 于 2015-1-1 16:57 编辑

109行那货不会被GC干掉么

↓ 当然不可能立即干掉的,又没有开 stress 模式

点评

不会呀, 虽然我没用变量赋值, 但是背景确实在那没有消失  发表于 2015-1-1 16:56
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部