Browse Source

Fixing opacity handling for UISceneView/UITextureWidget, additional asset handling

Josh Engebretson 10 years ago
parent
commit
247150f659

+ 27 - 4
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/ui/inspector/MaterialInspector.ts

@@ -22,6 +22,29 @@ class MaterialInspector extends ScriptWidget {
 
     }
 
+    getTextureThumbnail(texture: Atomic.Texture): Atomic.Texture {
+
+        if (!texture) return null;
+
+        var db = ToolCore.getAssetDatabase();
+        var asset = db.getAssetByPath(texture.name);
+
+        if (!asset)
+            return texture;
+
+        var thumbnail = asset.cachePath + "_thumbnail.png";
+        var cache = Atomic.getResourceCache();
+
+        var thumb = <Atomic.Texture2D> cache.getTempResource("Texture2D", thumbnail);
+
+
+        if (thumb)
+          return thumb;
+
+        return texture;
+
+    }
+
     createTextureSection(): Atomic.UISection {
 
         var section = new Atomic.UISection();
@@ -62,7 +85,7 @@ class MaterialInspector extends ScriptWidget {
             attrLayout.addChild(name);
 
             var textureWidget = new Atomic.UITextureWidget();
-            textureWidget.texture = this.material.getTexture(tunit);
+            textureWidget.texture = this.getTextureThumbnail(this.material.getTexture(tunit));
 
             var tlp = new Atomic.UILayoutParams();
             tlp.width = 32;
@@ -77,9 +100,9 @@ class MaterialInspector extends ScriptWidget {
 
             var callback = function(ev: Atomic.UIWidgetEvent) {
 
-                var tselect = new TextureSelector(ev.target.view);
-
-                print("CALLBACK: ", ev.target.getTypeName(), this.textureUnit);
+                if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
+                  var tselect = new TextureSelector(ev.target.view);
+              }
 
             }.bind(editInfo);
 

+ 43 - 3
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/ui/inspector/TextureSelector.ts

@@ -1,13 +1,13 @@
 
 class TextureSelector extends Atomic.UIWindow {
 
-    constructor(parent:Atomic.UIWidget) {
+    constructor(parent: Atomic.UIWidget) {
 
         super();
 
         this.text = "Select Texture";
 
-        this.rect = [0,0, 320, 512];
+        this.rect = [0, 0, 320, 512];
 
         var mainLayout = new Atomic.UILayout();
         mainLayout.gravity = Atomic.UI_GRAVITY_ALL;
@@ -25,7 +25,29 @@ class TextureSelector extends Atomic.UIWindow {
         scrollLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
         scrollLayout.axis = Atomic.UI_AXIS_Y;
 
-        scrollContainer.addChild(scrollLayout);
+        scrollContainer.contentRoot.addChild(scrollLayout);
+
+        var db = ToolCore.getAssetDatabase();
+
+        var textures = db.getAssetsByImporterType("TextureImporter");
+
+        for (var i in textures) {
+
+            var thumbnail = textures[i].cachePath + "_thumbnail.png";
+            var cache = Atomic.getResourceCache();
+
+            var textureWidget = new Atomic.UITextureWidget();
+            textureWidget.texture = <Atomic.Texture2D> cache.getTempResource("Texture2D", thumbnail);
+
+            var tlp = new Atomic.UILayoutParams();
+            tlp.width = 64;
+            tlp.height = 64;
+            textureWidget.layoutParams = tlp;
+
+            scrollLayout.addChild(textureWidget);
+
+        }
+
 
         mainLayout.addChild(scrollContainer);
 
@@ -33,6 +55,24 @@ class TextureSelector extends Atomic.UIWindow {
 
         this.center();
 
+        this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
+
+    }
+
+    handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
+
+        if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
+
+            if (ev.target != this && !this.isAncestorOf(ev.target)) {
+
+                //this.close();
+
+            }
+
+        }
+
+        return false;
+
     }
 
 }

+ 2 - 0
Source/Atomic/UI/UI.h

@@ -61,6 +61,8 @@ public:
 
     void GetTBIDString(unsigned id, String& value);
 
+    UIRenderer* GetRenderer() { return renderer_; }
+
 private:
 
     static WeakPtr<Context> uiContext_;

+ 19 - 2
Source/Atomic/UI/UISceneView.cpp

@@ -18,7 +18,9 @@
 #include <Atomic/Graphics/Renderer.h>
 #include <Atomic/Core/CoreEvents.h>
 
+#include "UIRenderer.h"
 #include "UISceneView.h"
+
 using namespace tb;
 
 namespace Atomic
@@ -30,6 +32,7 @@ UISceneView::UISceneView(Context* context, bool createWidget) : UIWidget(context
     size_(-1, -1),
     resizeRequired_(false)
 {
+    UI* ui= GetSubsystem<UI>();
 
     if (createWidget)
     {
@@ -42,12 +45,14 @@ UISceneView::UISceneView(Context* context, bool createWidget) : UIWidget(context
         widget_->SetGravity(WIDGET_GRAVITY_ALL);
         ((SceneViewWidget*)widget_)->sceneView_ = this;
 
-        GetSubsystem<UI>()->WrapWidget(this, widget_);
+        ui->WrapWidget(this, widget_);
 
 
     }
 
-   SubscribeToEvent(E_ENDFRAME, HANDLER(UISceneView, HandleEndFrame));
+    renderer_ = ui->GetRenderer();
+
+    SubscribeToEvent(E_ENDFRAME, HANDLER(UISceneView, HandleEndFrame));
 }
 
 UISceneView::~UISceneView()
@@ -205,6 +210,18 @@ void SceneViewWidget::OnPaint(const PaintProps &paint_props)
 
     float* data = &vertexData_[0];
 
+    float color;
+    float fopacity = GetOpacity() * sceneView_->renderer_->GetOpacity();
+    unsigned char opacity = (unsigned char) (fopacity* 255.0f);
+    ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
+
+    data[3] = color;
+    data[9] = color;
+    data[15] = color;
+    data[21] = color;
+    data[27] = color;
+    data[33] = color;
+
     data[0] = rect.x;
     data[1] = rect.y;
 

+ 6 - 0
Source/Atomic/UI/UISceneView.h

@@ -15,6 +15,7 @@ namespace Atomic
 {
 
 class UISceneView;
+class UIRenderer;
 
 class SceneViewWidget : public tb::TBWidget
 {
@@ -38,6 +39,9 @@ private:
 
 class UISceneView : public UIWidget
 {
+
+    friend class SceneViewWidget;
+
     OBJECT(UISceneView)
 
 public:
@@ -102,6 +106,8 @@ protected:
 
 private:
 
+    UIRenderer* renderer_;
+
 };
 
 }

+ 19 - 1
Source/Atomic/UI/UITextureWidget.cpp

@@ -7,6 +7,7 @@
 
 #include "UIEvents.h"
 #include "UI.h"
+#include "UIRenderer.h"
 #include "UITextureWidget.h"
 
 using namespace tb;
@@ -37,13 +38,17 @@ private:
 
 UITextureWidget::UITextureWidget(Context* context, bool createWidget) : UIWidget(context, false)
 {
+    UI* ui = GetSubsystem<UI>();
+
     if (createWidget)
     {
         widget_ = new TBTextureWidget();
         ((TBTextureWidget*)widget_)->uiTextureWidget_ = this;
         widget_->SetDelegate(this);
-        GetSubsystem<UI>()->WrapWidget(this, widget_);
+        ui->WrapWidget(this, widget_);
     }
+
+    renderer_ = ui->GetRenderer();
 }
 
 UITextureWidget::~UITextureWidget()
@@ -69,6 +74,7 @@ bool UITextureWidget::OnEvent(const tb::TBWidgetEvent &ev)
 TBTextureWidget::TBTextureWidget()
 {
     vertexData_.Resize(6 * UI_VERTEX_SIZE);
+
     float color;
     ((unsigned&)color) = 0xFFFFFFFF;
 
@@ -94,6 +100,18 @@ void TBTextureWidget::OnPaint(const PaintProps &paint_props)
 
     float* data = &vertexData_[0];
 
+    float color;
+    float fopacity = GetOpacity() * uiTextureWidget_->renderer_->GetOpacity();
+    unsigned char opacity = (unsigned char) (fopacity* 255.0f);
+    ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
+
+    data[3] = color;
+    data[9] = color;
+    data[15] = color;
+    data[21] = color;
+    data[27] = color;
+    data[33] = color;
+
     data[0] = rect.x;
     data[1] = rect.y;
 

+ 5 - 0
Source/Atomic/UI/UITextureWidget.h

@@ -7,11 +7,14 @@ namespace Atomic
 {
 
 class Texture;
+class UIRenderer;
 
 /// A widget that can render a Texture2D, so the image data
 /// doesn't need to be loaded 2x (once for Texture2D and once for say a UIImageWidget)
 class UITextureWidget : public UIWidget
 {
+    friend class TBTextureWidget;
+
     OBJECT(UITextureWidget)
 
 public:
@@ -30,6 +33,8 @@ private:
 
     SharedPtr<Texture> texture_;
 
+    UIRenderer* renderer_;
+
 };
 
 }

+ 10 - 0
Source/Atomic/UI/UIWindow.cpp

@@ -41,6 +41,16 @@ void UIWindow::ResizeToFitContent()
 
 }
 
+void UIWindow::Close()
+{
+    if (!widget_)
+        return;
+
+    ((TBWindow*)widget_)->Close();
+
+}
+
+
 void UIWindow::AddChild(UIWidget *child)
 {
     if (!widget_ || !child->GetInternalWidget())

+ 2 - 0
Source/Atomic/UI/UIWindow.h

@@ -22,6 +22,8 @@ public:
 
     void AddChild(UIWidget *child);
 
+    void Close();
+
 protected:
 
     virtual bool OnEvent(const tb::TBWidgetEvent &ev);

+ 2 - 1
Source/AtomicJS/Packages/ToolCore/ToolCore.json

@@ -8,7 +8,8 @@
 	"typescript_decl" : {
 
 		"AssetDatabase" : [
-			"getFolderAssets(folder:string):ToolCore.Asset[];"
+			"getFolderAssets(folder:string):ToolCore.Asset[];",
+			"getAssetsByImporterType(type:string):ToolCore.Asset[];"
 		]
 	}
 

+ 8 - 0
Source/ToolCore/Assets/Asset.cpp

@@ -199,6 +199,14 @@ bool Asset::CreateImporter()
 
 }
 
+String Asset::GetCachePath() const
+{
+    AssetDatabase* db = GetSubsystem<AssetDatabase>();
+    String cachePath = db->GetCachePath();
+    cachePath += guid_;
+    return cachePath;
+}
+
 
 bool Asset::SetPath(const String& path)
 {

+ 2 - 0
Source/ToolCore/Assets/Asset.h

@@ -31,8 +31,10 @@ public:
     const String& GetGUID() const { return guid_; }
     const String& GetName() const { return name_; }
     const String& GetPath() const { return path_; }
+    String GetCachePath() const;
     unsigned GetTimestamp() const { return timestamp_; }
 
+    const StringHash GetImporterType() { return importer_.Null() ? String::EMPTY : importer_->GetType(); }
     const String& GetImporterTypeName() { return importer_.Null() ? String::EMPTY : importer_->GetTypeName(); }
 
     void SetDirty(bool dirty) { dirty_ = dirty; }

+ 18 - 0
Source/ToolCore/Assets/AssetDatabase.cpp

@@ -274,6 +274,24 @@ void AssetDatabase::GetFolderAssets(String folder, PODVector<Asset*>& assets) co
 
 }
 
+void AssetDatabase::GetAssetsByImporterType(StringHash type, PODVector<Asset*>& assets) const
+{
+    assets.Clear();
+
+    List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
+
+    while (itr != assets_.End())
+    {
+        Asset* asset = *itr;
+
+        if (asset->GetImporterType() == type)
+            assets.Push(asset);
+
+        itr++;
+    }
+
+}
+
 void AssetDatabase::GetDirtyAssets(PODVector<Asset*>& assets)
 {
     assets.Clear();

+ 2 - 0
Source/ToolCore/Assets/AssetDatabase.h

@@ -33,6 +33,8 @@ public:
 
     void GetFolderAssets(String folder, PODVector<Asset*>& assets) const;
 
+    void GetAssetsByImporterType(StringHash type, PODVector<Asset*>& assets) const;
+
     void GetDirtyAssets(PODVector<Asset*>& assets);
 
     String GetDotAssetFilename(const String& path);

+ 29 - 2
Source/ToolCoreJS/ToolCoreJS.cpp

@@ -65,6 +65,33 @@ static int AssetDatabase_GetFolderAssets(duk_context* ctx)
     return 1;
 }
 
+static int AssetDatabase_GetAssetsByImporterType(duk_context* ctx)
+{
+    JSVM* vm = JSVM::GetJSVM(ctx);
+    ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
+    AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
+    Project* project = ts->GetProject();
+
+    StringHash type = duk_require_string(ctx, 0);
+
+    duk_push_array(ctx);
+
+    if (!project)
+        return 1;
+
+    PODVector<Asset*> assets;
+    db->GetAssetsByImporterType(type, assets);
+
+    for(unsigned i = 0; i < assets.Size(); i++)
+    {
+        js_push_class_object_instance(ctx, assets[i], 0);
+        duk_put_prop_index(ctx, -2, i);
+    }
+
+    return 1;
+}
+
+
 void jsapi_init_toolcore(JSVM* vm)
 {
     duk_context* ctx = vm->GetJSContext();
@@ -85,13 +112,13 @@ void jsapi_init_toolcore(JSVM* vm)
     duk_push_c_function(ctx, js_atomic_GetAssetDatabase, 0);
     duk_put_prop_string(ctx, -2, "getAssetDatabase");
 
-
     duk_pop(ctx);
 
-
     js_class_get_prototype(ctx, "ToolCore", "AssetDatabase");
     duk_push_c_function(ctx, AssetDatabase_GetFolderAssets, 1);
     duk_put_prop_string(ctx, -2, "getFolderAssets");
+    duk_push_c_function(ctx, AssetDatabase_GetAssetsByImporterType, 1);
+    duk_put_prop_string(ctx, -2, "getAssetsByImporterType");
     duk_pop(ctx);