设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2225|回复: 2
打印 上一主题 下一主题

[讨论] 一次界面脚本结构优化的经历

[复制链接]

Lv5.捕梦者

梦石
10
星屑
39440
在线时间
1914 小时
注册时间
2010-11-14
帖子
3315

R考场第七期纪念奖

跳转到指定楼层
1
发表于 2019-3-3 23:03:24 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

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

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

x
初学ruby时,用自己蹩脚的编程水平写了一个标题界面。


当然,代码的结构十分的“个性”,不利于阅读与修改,更不用说拓展了。
随便找出一段代码都能让人看着头大

旧代码


最近着手重构这部分代码.
优化过程

好了,这样可读性大幅度提高了,并且还有机会扩展。
新代码


最终效果
用头画头像,用脚写脚本

Lv3.寻梦者

梦石
0
星屑
4583
在线时间
1205 小时
注册时间
2016-4-7
帖子
982

开拓者

2
发表于 2019-3-4 00:12:53 | 只看该作者
本帖最后由 shitake 于 2019-3-4 02:10 编辑

为什么不来试试 rgui 呢 【逃

github.com/molingyu/rgui

https://molingyu.github.io/rgui/

rgui


上边界面的等价物 没测试 感觉是跑不起来 orz
RUBY 代码复制
  1. # require 'rgui'
  2.  
  3. class View_Title
  4.     include RGUI
  5.     def initialize
  6.         @childs = []
  7.         @bg = Component::ImageBox.new({
  8.             image: Bitmap.new('bg.png'),
  9.             x: 0,
  10.             y: 0,
  11.             width: Graphics.width,
  12.             height: Graphics.height,
  13.             type: Component::ImageBoxType::Filling
  14.         })
  15.         @childs.push @bg
  16.         @button_bg =  Component::ImageBox.new({
  17.             image: Bitmap.new(Graphics.width, 48).fill(0, 0, Graphics.width, 48, Color.new(255, 255, 255, 180)),
  18.             type: Component::ImageBoxType::Responsive,
  19.             visible: false
  20.         })
  21.         @button_bg.add_action(:breath, {speed: 0.6})
  22.         @childs.push @button_bg
  23.         @command_index = 0
  24.     end
  25.  
  26.     def create_command(name, method, enabled = false)
  27.         button = Component::SpriteButton.new({
  28.             x: 0,
  29.             y: 300 + @command_index * 48,
  30.             status: enabled,
  31.             images: create_button_bitmap(conf.name)
  32.         })
  33.  
  34.         button.on(:click){ |_| method.call }
  35.  
  36.         button.on(:mouse_out){ |em| em.object.lost_focus }
  37.  
  38.         button.on(:lost_focus) { |em|
  39.             em.object.sprite.zoom_y = 1
  40.             index = @childs.index(em.object)
  41.             while(@childs[index + 1].class == Component::SpriteButton)
  42.                 @childs[index + 1].y = @childs[index].y + @childs[index].height
  43.                 index += 1
  44.             end
  45.             @button_bg.hide
  46.         }
  47.  
  48.         button.on(:mouse_in){ |em|
  49.             Event::EventManager.focus_object.lost_focus if Event::EventManager.focus_object != em.object
  50.             Event::EventManager.focus_object == em.object
  51.             em.object.get_focus
  52.         }
  53.  
  54.         button.on(:get_focus){ |em|
  55.             em.object.sprite.zoom_y = 1.5
  56.             index = @childs.index(em.object)
  57.             while(@childs[index + 1].class == Component::SpriteButton)
  58.                 @childs[index + 1].y = @childs[index].y + @childs[index].height * 1.5
  59.                 index += 1
  60.             end
  61.             @button_bg.y = em.object.y
  62.             @button_bg.show
  63.         }
  64.         @childs.push(button)
  65.         @command_index += 1
  66.     end
  67.  
  68.     def create_button_bitmap(name)
  69.         font = Font.new("LilyUPC", 32)
  70.         font.bold = true
  71.         font.out_color = Color::BLACK
  72.         [Color::GL18, Color::GL32, Color::GL4].map{|c|
  73.             bitmap = Bitmap.new(Graphics.width, 32)
  74.             bitmap.font = font
  75.             bitmap.font.color = c
  76.             bitmap.draw_text(0, 0, Graphics.width, 32, name, 1)
  77.             bitmap
  78.          }
  79.     end
  80.  
  81.     def update
  82.         @childs.each{|c| c.update }
  83.     end
  84.  
  85.     def dispose
  86.         @childs.each{|c| c.dispose }
  87.     end
  88.  
  89. end
  90.  
  91. class Scene_Title < Scene_Base
  92.     def start
  93.         @view = View_Title.new
  94.         @view.create_command 'New Game', method :new_game
  95.         @view.create_command 'Load Game', method(:continue), DataManager.save_file_exists?
  96.         @view.create_command 'Shutdown', method :shutdown
  97.     end
  98.  
  99.     def update
  100.         @view.update
  101.     end
  102.  
  103.     def new_game
  104.     end
  105.  
  106.     def continue
  107.     end
  108.  
  109.     def shutdown
  110.     end
  111. end
附庸的附庸不是我的附庸,女儿的女儿还是我的女儿。CK2沉迷ing
回复 支持 反对

使用道具 举报

Lv5.捕梦者

梦石
10
星屑
39440
在线时间
1914 小时
注册时间
2010-11-14
帖子
3315

R考场第七期纪念奖

3
 楼主| 发表于 2019-3-4 00:57:58 | 只看该作者
shitake 发表于 2019-3-4 00:12
为什么不来试试 rgui 呢 【逃

github.com/molingyu/rgui

这个太触了,回头都可以用rm写简单的窗体程序了……

我还只是做着玩玩,用不到这么高端的东西233
(咱不是搞编程的,只会瞎摸几下rgss,涉及到线程和API就不会了_(:з」∠)_
用头画头像,用脚写脚本
回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-20 15:46

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表