Giter Club home page Giter Club logo

ofxcomponent's Introduction

ofxComponent

This is an addon for openFrameworks (oF) called ofxComponent. A convenient tool for creating UIs within oF was lacking, so this addon was created.

The purpose of this tool is to simplify the organization of object hierarchies (parent-child relationships) in UI components, such as using relative mouse coordinates within each object and organizing object position relationships within a relative coordinate system.

The drawing order is also considered, ensuring that parent objects are drawn before child objects, making it easier to create structures such as UI panels with buttons and views. Specifically, you can set relative positions using the setPos() method, obtain your own coordinates relative to the parent using getPos(), and obtain relative mouse coordinates within your coordinate system using getMousePos().

Depend addons

  • It doesn't depend on any other addons.

Tested system

  • of0.10.1 or later

Usage

To get started, please refer to the examples provided. To use this tool, you need to inherit all objects you create from the ofxComponentBase class and manage them using shared_ptr. Please use shared_ptr with caution, paying attention to its features. Establish parent-child relationships using the setChild() method to link parent and child objects. Parent-child relationships can change during runtime.

There is also an ofxComponentManager class to manage the entire object hierarchy. You need to create only one instance of this class and set it as the top-level parent:

// ofApp.h ofApp
shared_ptr<ofxComponentManager> manager;

Define one instance like this, and then:

// ofApp.cpp ofApp::setup()

manager->setChild(yourComponent);
// ...and more children...

manager->setup();

When removing an object, call destroy() and it will be removed during the next update. Since it is not removed immediately, you may need to use isDestroyed() to check whether the object has been removed as needed.

The ofxComponentBase class (header file provided) contains various methods and attributes to manage and control components, such as positioning, scaling, rotation, mouse and key events, and drawing constraints. It also includes methods for managing parent-child relationships, converting coordinate positions, and handling component destruction.

For more information on the usage and implementation details of ofxComponent, refer to the ofxComponentBase.h header file provided.

onUpdate, onDraw as Unity

When you override these methods, you can handle events.

  • onStart(); // is Called once before the first onUpdate() after the object is created.
  • onUpdate();
  • onDraw();

Input handler

  • onKeyPressed(ofKeyEventArgs&);
  • onKeyReleased(ofKeyEventArgs&);
  • onMouseMoved(ofMouseEventArgs&);
  • onMousePressed(ofMouseEventArgs&);
  • onMouseDragged(ofMouseEventArgs&);
  • onMouseReleased(ofMouseEventArgs&);
  • onDragEvent(ofDragInfo&);

Position, Herarchy, etc

  • setPos(ofVec2f pos); // To set the relative position of an object
  • getPos(); // To get an object's position relative to its parent
  • getMousePos(); // To get the mouse position relative to the object's coordinate system
  • setChild(shared_ptr<ofxComponentBase> child); // To establish a parent-child relationship between objects
  • getChildren(); // To get a list of child objects
  • setActive(bool active); // To set an object as active or inactive

Sample

MyComponent sample

#include "ofxComponent.h"
using namespace ofxComponent;

class MyComponent : public ofxComponentBase {
	void onStart() override {
		// set component size
		setWidth(200);
		setHeight(100);

		// set relative position
		setPos(50, 50);
	}
	void onDraw() override {
		// draw background
		ofSetColor(ofColor::yellow);
		// getWidth(), getHeight() returns component size.
		ofDrawRectangle(0, 0, getWidth(), getHeight());
	}
	void onMousePressedOverComponent(ofMouseEventArgs& mouse) {
		ofLog() << "Clicked";
	}
};

ofApp

Include this and use namespace "ofxComponent".

Instance of ofxComponentManager is need to run component system.

#include "ofxComponent.h"
using namespace ofxComponent;

class ofApp : public ofBaseApp{

    ... some definissions ...

    // ofxComponent needs this instance
    shared_ptr<ofxComponent::ofxComponentManager> manager;
};

Set up the componentManager and add MyComponent as a child.

void ofApp::setup(){
	// Set drawing area
	manager = make_shared<ofxComponentManager>();

	myComp = make_shared<MyComponent>();

	// Add children
	manager->addChild(myComp);

	// Setup all objects
	// Please call this after children add
	manager->setup();
}

This is in development, so there may be major changes from time to time.

Author

http://github.com/tettou771

Lisence

MIT

ofxcomponent's People

Contributors

tettou771 avatar

Stargazers

2bit avatar Víctor RG avatar Kyosuke Miyamura avatar nariakiiwatani avatar moebiusSurfing avatar

Watchers

James Cloos avatar Víctor RG avatar  avatar

ofxcomponent's Issues

errors in macOS / OF master branch

hey @tettou771 , thanks for sharing.

do you think it could be easy to repair this for macOS?
it seems it's not detecting the namespace to get the methods from the addon...

Error:Build failed with 20 errors and 2 warnings in 11 s 678 ms
ofApp.h
Error:(9, 32) reference to 'Component' is ambiguous
Error:(9, 32) base specifier must name a class
Error:(10, 17) only virtual member functions can be marked 'override'
Error:(15, 16) only virtual member functions can be marked 'override'
Error:(11, 3) use of undeclared identifier 'setRect'
Error:(12, 3) use of undeclared identifier 'setRotation'
Error:(13, 3) use of undeclared identifier 'setScale'
Error:(17, 25) use of undeclared identifier 'getWidth'
Error:(17, 37) use of undeclared identifier 'getHeight'; did you mean 'ofGetHeight'?
Error:(20, 11) use of undeclared identifier 'getMouseX'; did you mean 'ofGetMouseX'?
Error:(20, 26) use of undeclared identifier 'getMouseX'; did you mean 'ofGetMouseX'?
Error:(20, 40) use of undeclared identifier 'getWidth'
Error:(21, 11) use of undeclared identifier 'getMouseY'; did you mean 'ofGetMouseY'?
Error:(21, 26) use of undeclared identifier 'getMouseY'; did you mean 'ofGetMouseY'?
Error:(21, 40) use of undeclared identifier 'getHeight'; did you mean 'ofGetHeight'?
Error:(23, 17) use of undeclared identifier 'getMouseX'; did you mean 'ofGetMouseX'?
Error:(23, 30) use of undeclared identifier 'getMouseY'; did you mean 'ofGetMouseY'?
Error:(26, 25) use of undeclared identifier 'getMousePos'
Error:(27, 21) use of undeclared identifier 'localToGlobalPos'
ofApp.cpp
Error:too many errors emitted, stopping now [-ferror-limit=]
main.cpp
Error:too many errors emitted, stopping now [-ferror-limit=]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.