如何使对象在以表单呈现时“可缩放”

2024-05-21

我正在 Winform 中渲染我的游戏,方式与本示例中的方式相同:WinForms系列1:图形设备 http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1

在我的游戏中,我有一些对象,例如一个矩形,一旦创建,我就可以在我的游戏世界中放置和移动它。我的项目是一个关卡编辑器。

What I want to do is to make every object "sizable" or "scalable" (sorry if this isn't the correct word) in the same way as done in every software we commonly use, I mean: enter image description here

我有一堂课,比如:

public abstract class GameObject
{
    protected Vector2 position_ = Vector2.Zero;
    protected float rotation_ = 0.0f;
    protected Vector2 scale_ = Vector2.One;
    protected float depth_ = 0.0f;

    protected bool is_passable_ = true;

    protected GameObject(
        Vector2 starting_position)
    {
        this.position_ = starting_position;
    }

    [DisplayName("Position")]
    public virtual Vector2 Position
    {
        get { return position_; }
        set { position_ = value; }
    }

    [BrowsableAttribute(false)]
    public abstract Rectangle PositionRectangle
    {
        get;
    }

    [BrowsableAttribute(false)]
    public abstract Rectangle SelectionRectangle
    {
        get;
    }

    [DisplayName("Scale")]
    public abstract Vector2 Scale
    {
        get;
        set;
    }

    [BrowsableAttribute(false)]
    public virtual float Depth
    {
        get { return depth_; }
        set { depth_ = value; }
    }

    [DisplayName("IsPassable?")]
    public bool IsPassable
    {
        get { return is_passable_; }
        set { is_passable_ = value; }
    }

    [BrowsableAttribute(false)]
    public abstract Vector2 TextureSize
    {
        get;
    }

    public abstract void Update(GameTime gameTime);
    public abstract void Draw(SpriteBatch spriteBatch);
}

一旦类被实例化,我想在表单内执行类似的操作:(gameWrapper 是使用示例创建的控件,用于在表单内绘制游戏)

private void gameWrapper_MouseClick_1(object sender, MouseEventArgs e)
{
    Vector2 mouse_xy = new Vector2(e.Location.X, e.Location.Y);
    GameObject obj = gameWrapper.GetObjectByPosition(mouse_xy);
    propertyGrid1.SelectedObject = obj;
    if (obj != null)
        gameWrapper.SelectObject(obj);
    else gameWrapper.Unselect();

    propertyGrid1.Refresh();
 }

游戏包装内部:

public SelectObject(GameObject obj)
{
     List<Vector2> 4verticesList = new List();
     //
     // Code to add 4 vertices coordinates of the SelectionRectangle to 4verticesList
     //         

     foreach (Vector2 vertex_xy in 4VerticesList)
        DrawLittleRectangle(vertex_xy);
}

这正是我想做的。使用函数绘制小按钮/矩形,然后处理对它们的点击。

是否已经编写了一些代码来实现此行为?实际上,我并不担心对象将如何调整大小,而只是担心美观按钮。


我已经做到了!

主要功能:

private void DrawObjectSelection(SpriteBatch spriteBatch, Rectangle r)
{
    r = Telecamera.Transform(r);

    Vector2 v1 = new Vector2(r.X, r.Y);
    Vector2 v2 = new Vector2(r.X + r.Width, r.Y);
    Vector2 v3 = new Vector2(r.X, r.Y + r.Height);
    Vector2 v4 = new Vector2(r.X + r.Width, r.Y + r.Height);

    //The side rectangle
    DrawEmptyRectangle(spriteBatch, v1, v2, v3, v4);

    //4 squares at vertices
    DrawFilledSquare(spriteBatch, v1);
    DrawFilledSquare(spriteBatch, v2);
    DrawFilledSquare(spriteBatch, v3);
    DrawFilledSquare(spriteBatch, v4);

    //the other 4 in the middle of every side
    float height = v4.Y - v1.Y;
    float width = v2.X - v1.X;

    DrawFilledSquare(spriteBatch, new Vector2(v1.X, v1.Y + height / 2));
    DrawFilledSquare(spriteBatch, new Vector2(v2.X, v2.Y + height / 2));
    DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v1.Y));
    DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v4.Y));
}

具有这些功能:

private void DrawLine(
    SpriteBatch spriteBatch,
    float width,
    Vector2 point1, Vector2 point2)
{
    float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
    float length = Vector2.Distance(point1, point2);

    spriteBatch.Draw(
        grid_texture,
        point1,
        null,
        selection_color,
        angle,
        Vector2.Zero,
        new Vector2(length, width),
        SpriteEffects.None,
        0);
}

private void DrawEmptyRectangle(
    SpriteBatch spriteBatch,
    Vector2 v1,
    Vector2 v2,
    Vector2 v3,
    Vector2 v4)
{
    /*
     * V1****V2
     * *      *
     * *      *
     * V3****V4
     */

    DrawLine(spriteBatch, 1.0f, v1, v2);
    DrawLine(spriteBatch, 1.0f, v1, v3);
    DrawLine(spriteBatch, 1.0f, v2, v4);
    DrawLine(spriteBatch, 1.0f, v3, v4);
}

private void DrawFilledSquare(
    SpriteBatch spriteBatch,
    Vector2 c_pos) //With center position
{
    int lato = 8;

    spriteBatch.Draw(
        grid_texture,
        new Rectangle(
            (int)c_pos.X - lato/2,
            (int)c_pos.Y - lato/2,
            lato,
            lato),
            selection_color);

}

当我想绘制它时,我只需要一个矩形,例如:

//...

DrawObjectSelection(spriteBatch, Camera.Transform(gameObject1.PositionRectangle));

//...

唯一缺少的是处理每个方块上的点击。这是通过使用鼠标位置坐标进行简单的“包含”测试来完成的。

结果屏幕:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使对象在以表单呈现时“可缩放” 的相关文章

随机推荐

  • Git 工作流程:分叉项目并维护本地修改副本,但保持最新

    我正在尝试找出最佳工作流程 用于维护具有自定义功能的 github 托管项目 moodle 的本地副本 同时保持保持副本最新的能力 告诉我我正在考虑做的事情是否完全疯狂 分叉项目 github com moodle moodle gt gi
  • 将暂停屏幕绘制为播放屏幕上的一层 -LibGdx

    在我的 LibGdx 游戏中 我创建了暂停功能 在玩游戏时 如果我按下暂停按钮 则会显示一个带有恢复按钮的单独屏幕 实际上我想做的是暂停屏幕应该像一层一样出现在游戏屏幕上方 就像下面的游戏截图一样 我只能在我的游戏中使用单独的背景和所有内容
  • 使用“ember-rails”将路由从 Rails 迁移到现有 Rails 应用程序的 Ember

    将 gem ember rails 用于现有的 Rails 应用程序 我正在尝试使用 Ember 路由一个资源 很多人告诉我这段代码应该可以工作 但事实并非如此 我想突破学习曲线并使这项工作成功 但我需要一些帮助 Error Routing
  • ipython 笔记本锚链接直接从外部引用单元格

    我正在为基于笔记本的框架编写文档 当引用演示笔记本中的重要单元格时 我可以使用某种锚点来指向特定单元格吗 例如 如果我的演示笔记本位于 127 0 0 1 mydemo 是否可以通过某些锚标记 如 127 0 0 1 mydemo In10
  • Objective-C 中 typedef 枚举语句在哪里?

    我担心的一个基本问题 以下代码有效 并且 typedef 枚举被识别 但我收到一条警告消息 空声明中无用的存储类说明符 我在这里做错了什么吗 这是放置 typedef 枚举的最佳位置吗 import
  • WordPress 安装中发现的恶意 PHP 代码有什么作用?

    我能够解码在一些 WordPress 文件中找到的以下 PHP 脚本 只是出于好奇 有人可以告诉我这段代码实际上是做什么的吗 看起来它已经以某种方式复制到同一服务器上的其他 WordPress 安装中
  • 使用 Python 在 Django 中将 Unix 时间戳转换为人类格式

    我想将字符串中的 unix 时间戳 例如 1277722499 82 转换为更人性化的格式 hh mm ss 或类似格式 有没有一种简单的方法可以在 python 中为 django 应用程序执行此操作 这是在模板之外 在我想要执行此操作的
  • 如何在 JSFiddle 中链接外部 json 文件?

    我有一个很长的 json 文件country json name WORLD population 6916183000 name More developed regions population 1240935000 name Less
  • 使用GCD实现并发读独占写模型

    我试图了解使用 Grand Central Dispatch GCD 实现控制资源访问的并发读独占写模型的正确方法 假设有一个 NSMutableDictionary 被大量读取并且偶尔更新 确保读取始终与字典状态一致的正确方法是什么 当然
  • 如何将参数传递给 XML 视图 SAP UI5 中的事件处理程序

    我在将数据从 XML 视图发送到控制器时遇到问题 在 JS 视图中很容易实现 例如 在 JS 看来 var btn new sap m Button text click tap function callFunction oEvent m
  • 为什么编译器会警告 set precision 中的隐式转换?

    当我编译以下代码时 编译器给出警告 Implicit conversion loses integer precision std streamsize aka long to int 我对此警告有点困惑 因为我只是尝试保存精度的当前值 以
  • C 的二进制流解析库 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 您能推荐一个经过验证的 C 二进制流解析库吗 如果它能像 C 语言所允许的那样具有声明性 那就太好了
  • 为什么这个脚本永远不会运行?

    我有以下 JavaScript 语句 该语句在页面加载时执行 变量u1使用以下值之一填充 BBSLoan Accept PPI No 60Months BBSLoan Refer PPI No 60Months HSBSLoan Accep
  • 使用php在html页面中显示bbcode

    我已经有一个 bbcode 字符串 mybbcode b Hello word b 使用 php 我想在 html 页面中以 html 格式显示它 例如 div gt b hello word b div 基本上其他人已经对你说过了 但是如
  • Android中BaseColumns有什么用

    实现一个类有什么用BaseColumns在安卓中 The BaseColumns http developer android com reference android provider BaseColumns html接口提供了非常常见
  • 类型错误:“float”对象不可下标

    PizzaChange float input What would you like the new price for all standard pizzas to be PriceList 0 1 2 3 4 5 6 PizzaCha
  • 测试文本字段中的 double 是否有值

    尝试检查从文本字段获得的双变量是否有值 让值 双倍 Double valueTextfield text if value isEmpty X if 值 nil X 如果 值 0 X 我该怎么做呢 您可以使用 Double 的 init 方
  • 如何使用 c++ libboost 运行进程并获取其输出?

    我正在尝试运行外部 shell 命令并使用 C 的 Boost 库读取其输出 但似乎该命令未运行或我无法访问输出 我在用着他们的文档 https www boost org doc libs 1 65 1 doc html boost pr
  • wp_unregister 和 wp_enqueue

    有人建议我使用 wp unregister 和 wp enqueue 将 wordpress jquery 库替换为 google 托管的库 因为 wordpress 有一些问题 然而 当我尝试将这些插入我的 WordPress 网站时 它
  • 如何使对象在以表单呈现时“可缩放”

    我正在 Winform 中渲染我的游戏 方式与本示例中的方式相同 WinForms系列1 图形设备 http xbox create msdn com en US education catalog sample winforms seri